// 获取Home模块tabs标签
import { useEffect, useState } from "react";

import type { ChannelItem } from "@/apis/list"
import { fetchChannelsAPI } from "@/apis/list"

export function useTabs() {
        // useState是泛型约束
    const [channels, setChannels] = useState<ChannelItem[]>([])    // 状态变量

    // 副作用函数，获取tab标签列表
    useEffect(() => {
        const getChannels = async () => {
            // try 块：尝试执行可能出错的代码
            try {
                const res = await fetchChannelsAPI()
                setChannels(res.data.data.channels)
            }
            // catch 块：捕获并处理错误
            catch (error) {
                // 抛出带自定义消息的错误
                throw new Error('获取tab标签列表失败')
            }
        }
        getChannels()
    }, [])

    // 返回数组或对象
    return {
        channels
    }
}